package com.wgke.kmmapp

import com.github.aakira.napier.Napier
import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.features.logging.*
import okhttp3.internal.ignoreIoExceptions
import java.util.concurrent.TimeUnit

/**android请求引擎*/
fun AndroidHttpClient(withLog: Boolean) = HttpClient(OkHttp) {
    engine {
        config {
            retryOnConnectionFailure(true)
            connectTimeout(5, TimeUnit.SECONDS)
        }
    }
    if (withLog) install(Logging) {
        level = LogLevel.HEADERS
        logger = object : Logger {
            override fun log(message: String) {
                Napier.v(tag = "AndroidHttpClient", message = message)
            }
        }
    }
//    install(JsonFeature) {
//        serializer = KotlinxSerializer()
//        ignoreIoExceptions {  }
//    }
    Json {
        serializer=KotlinxSerializer()

    }

}